package org.agridata.akvisualization.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.agridata.akvisualization.doMain.vo.AnimalSaveCropVo;
import org.agridata.akvisualization.doMain.vo.ChulanVo;
import org.agridata.akvisualization.doMain.vo.OriginVo;
import org.agridata.akvisualization.mapper.AnimalSaveCropMapper;
import org.agridata.akvisualization.mapper.QuarantineStatisticsMapper;
import org.agridata.akvisualization.result.Result;
import org.agridata.akvisualization.service.AnimalSaveCropService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author: yxc
 * @date: 2024/2/1 16:39
 * @Description:
 */
@Service
public class AnimalSaveCropServiceImpl implements AnimalSaveCropService {

    @Autowired
    AnimalSaveCropMapper animalSaveCropMapper;

    @Autowired
    private QuarantineStatisticsMapper quarantineStatisticsMapper;

    @Autowired
    QuarantineStatisticsServiceImpl quarantineStatisticsService;

    @Override
    public Result importAnimalSaveCrop(List<AnimalSaveCropVo> memberList, String dates, String quarter) {
        int successCount = 0;
        int failureCount = 0;

        List<Map<String,Object>> SaveCrop = animalSaveCropMapper.selectSaveCropByDates(dates, quarter);
        if (SaveCrop.size() > 0) {
            animalSaveCropMapper.deleteSaveCrop(SaveCrop);
        }

        for (AnimalSaveCropVo animalSaveCrop : memberList) {
            try {
                animalSaveCrop.setDate(dates);
                animalSaveCrop.setQuarter(quarter);
                animalSaveCropMapper.insertAnimalSaveCrop(animalSaveCrop);
                successCount++;
            } catch (Exception e) {
                failureCount++;
                e.printStackTrace();
            }
        }
        return Result.ok("成功："+ successCount+ "条"+"，"+"失败"+failureCount+"条");
    }

    @Override
    public Page<AnimalSaveCropVo> getAnimalList(String data, String quarter, Long pageNum, Long pageSize) {
        Page<AnimalSaveCropVo> page = new Page<>(pageNum,pageSize);
        Page<AnimalSaveCropVo> animalList = animalSaveCropMapper.getAnimalList(page, data, quarter);
        return animalList;
    }

    @Override
    public Map<String, Object> getAnimalRatio(String type) {
        Map<String, Object> sMap = new HashMap<>();
        if ("1".contains(type)){
            List<Map<String,Object>> chunlan = animalSaveCropMapper.selectImportchunlanDates();
            sMap.put("chunlan",chunlan);
            List<Map<String,Object>> chulan = animalSaveCropMapper.selectImportchulanDates();
            sMap.put("chulan",chulan);
            return sMap;
        }
        if ("2".contains(type)){
            List<OriginVo> originVos1 = animalSaveCropMapper.selectchunlanDates();
            List<OriginVo> chunlan = quarantineStatisticsService.zhengHeOrigin(originVos1);
            sMap.put("chunlan",chunlan);

            List<ChulanVo> chulanVos = animalSaveCropMapper.selectchulanDates();
            List<Map<String, Object>> maps = zhengHeOrigin(chulanVos);
            sMap.put("chulan",maps);
            return sMap;
        }
        sMap.put("code",500);
        sMap.put("msg","type类型为空！");
        return sMap;
    }

    public List<Map<String,Object>> zhengHeOrigin(List<ChulanVo> originList) {
        List<ChulanVo> originLists = new ArrayList<>();
        List<Map<String,Object>> result = new ArrayList<>();


        Map<String, Object> jidu1 = new HashMap<>();
        Map<String, Object> jidu2 = new HashMap<>();
        Map<String, Object> jidu3 = new HashMap<>();
        Map<String, Object> jidu4 = new HashMap<>();

        Long zhu1 = 0L;
        Long zhu2 = 0L;
        Long zhu3 = 0L;
        Long zhu4 = 0L;

        Long niu1 = 0L;
        Long niu2 = 0L;
        Long niu3 = 0L;
        Long niu4 = 0L;

        Long yang1 = 0L;
        Long yang2 = 0L;
        Long yang3 = 0L;
        Long yang4 = 0L;

        Long qing1 = 0L;
        Long qing2 = 0L;
        Long qing3 = 0L;
        Long qing4 = 0L;

        //组其他禽类数据
        for (ChulanVo originVo : originList) {
            if (originVo.getAnimalID() == 0||originVo.getAnimalID()==16||originVo.getAnimalID()==10000||originVo.getAnimalID()==38||originVo.getAnimalID()==13
                    ||originVo.getAnimalID()==99913||originVo.getAnimalID()==30000||originVo.getAnimalID()==70000||originVo.getAnimalID()==4||originVo.getAnimalID()==43||originVo.getAnimalID()==40000){
                originVo.setAnimalName("其它");
                originVo.setAnimalID(0);
            }
            if (originVo.getAnimalID() == 8 || originVo.getAnimalID() == 7 || originVo.getAnimalID() == 6 || originVo.getAnimalID() == 20400 || originVo.getAnimalID() == 9 || originVo.getAnimalID() == 29900) {
                originVo.setAnimalName("禽");
                originVo.setAnimalID(20000);
            }
            originLists.add(originVo);
        }
        //根据季度计算猪牛羊禽出栏量
        for (ChulanVo originVos : originLists){
            if (originVos.getQuarter().equals(1)){
                if (originVos.getAnimalName().contains("猪")){
                    zhu1 += originVos.getCounts();
                }
                if (originVos.getAnimalName().contains("牛")){
                    niu1 += originVos.getCounts();
                }
                if (originVos.getAnimalName().contains("羊")){
                    yang1 += originVos.getCounts();
                }
                if (originVos.getAnimalName().contains("禽")){
                    qing1 += originVos.getCounts();
                }

            }
            if (originVos.getQuarter().equals(2)){
                if (originVos.getAnimalName().contains("猪")){
                    zhu2 += originVos.getCounts();
                }
                if (originVos.getAnimalName().contains("牛")){
                    niu2 += originVos.getCounts();
                }
                if (originVos.getAnimalName().contains("羊")){
                    yang2 += originVos.getCounts();
                }
                if (originVos.getAnimalName().contains("禽")){
                    qing2 += originVos.getCounts();
                }
            }
            if (originVos.getQuarter().equals(3)){
                if (originVos.getAnimalName().contains("猪")){
                    zhu3 += originVos.getCounts();
                }
                if (originVos.getAnimalName().contains("牛")){
                    niu3 += originVos.getCounts();
                }
                if (originVos.getAnimalName().contains("羊")){
                    yang3 += originVos.getCounts();
                }
                if (originVos.getAnimalName().contains("禽")){
                    qing3 += originVos.getCounts();
                }
            }
            if (originVos.getQuarter().equals(4)){
                if (originVos.getAnimalName().contains("猪")){
                    zhu4 += originVos.getCounts();
                }
                if (originVos.getAnimalName().contains("牛")){
                    niu4 += originVos.getCounts();
                }
                if (originVos.getAnimalName().contains("羊")){
                    yang4 += originVos.getCounts();
                }
                if (originVos.getAnimalName().contains("禽")){
                    qing4 += originVos.getCounts();
                }
            }
        }

        jidu1.put("zhu",zhu1);
        jidu1.put("niu",niu1);
        jidu1.put("yang",yang1);
        jidu1.put("qing",qing1);
        result.add(jidu1);

        jidu2.put("zhu",zhu2);
        jidu2.put("niu",niu2);
        jidu2.put("yang",yang2);
        jidu2.put("qing",qing2);
        result.add(jidu2);

        jidu3.put("zhu",zhu3);
        jidu3.put("niu",niu3);
        jidu3.put("yang",yang3);
        jidu3.put("qing",qing3);
        result.add(jidu3);

        jidu4.put("zhu",zhu4);
        jidu4.put("niu",niu4);
        jidu4.put("yang",yang4);
        jidu4.put("qing",qing4);
        result.add(jidu4);

        return result;
    }
}
